package com.sixth.controller;

import com.sixth.bean.bo.*;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.EditTopicVO;
import com.sixth.bean.vo.MarketTopicVO;
import com.sixth.bean.vo.TopicVO;
import com.sixth.constant.Constant;
import com.sixth.service.TopicService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author Chen
 * @Date 2022-09-08  14-29-53
 * @description 专题管理
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {


    @Autowired
    TopicService topicService;

    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-08  14-30-59
     * @description 查询专题表：market_topic
     */
    @RequiresPermissions(value = {"admin:topic:list"},logical = Logical.OR)
    @GetMapping("list")
    public BaseRespVo getTopicList(TopicPageInfo topicPageInfo) {

        BasePageInfoVo<MarketTopicVO> topicList = (BasePageInfoVo<MarketTopicVO>) topicService.getTopicList(topicPageInfo);

        if (topicList.getList().size() >= Constant.ONE) {
            return BaseRespVo.ok(topicList);
        }

        return BaseRespVo.invalidParameter("暂无数据");
    }


    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-08  16-31-39
     * @description 批量删除专题
     */
    @RequiresPermissions(value = {"admin:topic:batch-delete"},logical = Logical.OR)
    @PostMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody TopicBatchDeleteBO ids) {

        int delCode = topicService.batchDelTopic(ids);

        if (delCode == Constant.OK) {
            return BaseRespVo.ok("删除成功");
        }

        return BaseRespVo.invalidParameter("删除失败");
    }




    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-08  19-33-56
     * @description 根据ID删除一个专题
     */

    @RequiresPermissions(value = {"admin:topic:delete"},logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delTopic(@RequestBody DelTopicBO delTopicBO) {

        int delCode = topicService.deleteTopic(delTopicBO);

        if (delCode == Constant.OK) {
            return BaseRespVo.ok("删除成功");
        }

        return BaseRespVo.invalidParameter("删除失败");
    }

    /**
     * @param
     * @return {@link null }
     * @Author Chen
     * @Date 2022-09-08  19-37-57
     * @description 添加专题
     */
    @PostMapping("create")
    public BaseRespVo createTopin(@RequestBody TopicBO topicBO) {

        TopicVO topicVO = topicService.addTopic(topicBO);

        if (topicVO != null){
            return BaseRespVo.ok(topicVO);
        }

        return BaseRespVo.invalidParameter("添加失败");
    }

    /**
     * @Author Chen
     * @Date 2022-09-08  23-09-17
     * @param
     * @return {@link null }
     * @description 根据ID获取专题信息
     */
    @GetMapping("read")
    public BaseRespVo getTopic(Integer id){

        EditTopicVO editTopicVO = topicService.getTopic(id);

        if (editTopicVO.getTopic()!=null){

            return BaseRespVo.ok(editTopicVO);
        }
        return BaseRespVo.invalidParameter("查询失败");
    }

    /**
     * @Author Chen
     * @Date 2022-09-09  10-08-38
     * @param
     * @return {@link null }
     * @description 更新专题
     */
    @PostMapping("update")
    public BaseRespVo updateTopic(@RequestBody EditTopicBO editTopicBO){

        MarketTopicVO topicVO = topicService.updateTopic(editTopicBO);

        if (topicVO!=null){
            return BaseRespVo.ok(topicVO);

        }

        return BaseRespVo.invalidParameter("更新失败");
    }
}
